namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    public class PaintLinePeriodicLine : PaintLineBase
    {
        private int oriPeriod = 1;
        private int period = 5;

        public PaintLinePeriodicLine()
        {
            base.LineType = PaintLineType.Rectangle;
            base.PlName_zh = "等周期线";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                float num = div.Rect.Bottom - div.CalenderScale.Height;
                base.CgInstance.Cursor = Cursors.Hand;
                foreach (int num3 in this.LineParam())
                {
                    float x = base.CgInstance.GetX(num3);
                    if (((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num - (base.LineWidth * 5f))) && (crossHairPoint.Y <= num)))
                    {
                        base.CgInstance.Cursor = Cursors.SizeNS;
                        base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num3][base.CgInstance.CalenderScaleField].ToString(), 0.0);
                        base.MoveType = 1;
                        this.oriPeriod = this.period;
                    }
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                g.SmoothingMode = SmoothingMode.AntiAlias;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(brush) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                double[] numArray = this.LineParam(pList);
                for (int i = 0; i < numArray.Length; i++)
                {
                    int pos = (int) numArray[i];
                    float num3 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                    g.DrawLine(pen, num3, 0f, num3, (float) base.CgInstance.GetWorkSpaceY(div.PanelID));
                    if (i == (numArray.Length - 1))
                    {
                        g.DrawEllipse(pen, num3, base.CgInstance.GetWorkSpaceY(div.PanelID) - ((this.period * base.CgInstance.XScaleSpace) / 2f), this.period * base.CgInstance.XScaleSpace, this.period * base.CgInstance.XScaleSpace);
                    }
                    g.DrawEllipse(pen, (float) (num3 - (this.period * base.CgInstance.XScaleSpace)), (float) (base.CgInstance.GetWorkSpaceY(div.PanelID) - ((this.period * base.CgInstance.XScaleSpace) / 2f)), (float) (this.period * base.CgInstance.XScaleSpace), (float) (this.period * base.CgInstance.XScaleSpace));
                    if (base.Selected)
                    {
                        g.FillRectangle(brush, new RectangleF(num3 - (base.LineWidth * 2.5f), base.CgInstance.GetWorkSpaceY(div.PanelID) - (base.LineWidth * 5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    }
                }
                brush.Dispose();
                pen.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count <= 0)
            {
                return false;
            }
            int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
            double valueByPoint = base.CgInstance.GetValueByPoint(mp);
            string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
            base.PointList.Clear();
            base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
            this.period = base.CgInstance.GetMaxVisibleRecord() / 10;
            if (this.period < 1)
            {
                this.period = 1;
            }
            return true;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                double[] numArray = this.LineParam();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                float num = div.Rect.Bottom - div.CalenderScale.Height;
                for (int i = 0; i < numArray.Length; i++)
                {
                    int pos = (int) numArray[i];
                    float x = base.CgInstance.GetX(pos);
                    if ((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f))))
                    {
                        return true;
                    }
                    float num5 = x - ((this.period * base.CgInstance.XScaleSpace) / 2f);
                    float num6 = num;
                    float num7 = crossHairPoint.X - num5;
                    float num8 = crossHairPoint.Y - num6;
                    float num9 = (this.period * base.CgInstance.XScaleSpace) / 2f;
                    if (((((num7 * num7) + (num8 * num8)) / (num9 * num9)) >= 0.9) && ((((num7 * num7) + (num8 * num8)) / (num9 * num9)) <= 1.1))
                    {
                        return true;
                    }
                    if (i == (numArray.Length - 1))
                    {
                        num5 = x + ((this.period * base.CgInstance.XScaleSpace) / 2f);
                        if (((((num7 * num7) + (num8 * num8)) / (num9 * num9)) >= 0.9) && ((((num7 * num7) + (num8 * num8)) / (num9 * num9)) <= 1.1))
                        {
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = pList[0];
            double num1 = value2.Value;
            PaintLineBase.PointValue value3 = pList[0];
            int num = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            List<double> list = new List<double>();
            for (int i = base.CgInstance.FirstVisibleRecord - 1; i < base.CgInstance.LastVisibleRecord; i++)
            {
                if ((Math.Abs((int) (i - num)) % this.period) == 0)
                {
                    list.Add((double) i);
                }
            }
            return list.ToArray();
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                    int num3 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                    PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                    int num4 = (base.CgInstance.GetRecordByTime(value2.Key) + (num3 - indexByPoint)) - 1;
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num5 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num6 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num7 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num8 = (num5 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num7 - num6);
                            if (num4 >= 0)
                            {
                                if (num4 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num4 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num4 = 0;
                            }
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num4][base.CgInstance.CalenderScaleField].ToString(), value3.Value + num8);
                            return;
                        }
                        case 1:
                            this.period = this.oriPeriod + (num3 - indexByPoint);
                            if (this.period < 1)
                            {
                                this.period = 1;
                            }
                            return;
                    }
                }
            }
        }
    }
}

